package com.pethome.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pethome.common.core.domain.AjaxResult;
import com.pethome.web.domain.Post;
import com.pethome.web.domain.vo.PostAllInfo;
import com.pethome.web.domain.vo.PostAndCustomer;
import com.pethome.web.mapper.PostMapper;
import com.pethome.web.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 帖子管理Service业务层处理
 * 
 * @author 胡继承
 * @date 2024-09-04
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper,Post> implements IPostService
{
    @Autowired
    private PostMapper postMapper;

    /**
     * 获取全部文章信息和文章对应的顾客信息
    * @return
     */
    @Override
    public List<PostAndCustomer> selectPostAndCustomerList() {
        return postMapper.getPostListAndCustomer();
    }
    //查看 信息功能
    @Override
    public PostAllInfo selectPostAllInfoByPostId(Integer postId) {
        return postMapper.getPostAllInfo(postId);
    }
    //搜索功能
    @Override
    public List<PostAndCustomer> searchPost(String postTitle) {
        return postMapper.searchPost(postTitle);
    }
    //添加阅读量
    @Override
    public AjaxResult addReadCounnt(Integer postId) {
//        todo 脏读问题待解决
        log.warn(postId.toString());
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("post_id",postId);
        Post post = this.getOne(queryWrapper);
        post.setPostReadCount(post.getPostReadCount()+1);
        int i = postMapper.updatePost(post);
        return i==1?AjaxResult.success("成功"):AjaxResult.error("失败");
    }
    //获取顾客发布的全部文章
    @Override
    public List<Post> searchPostByCustomerId(Integer customerId) {
        QueryWrapper<Post> qw = new QueryWrapper<>();
        qw.eq("post_customer_id",customerId);
        return  this.list(qw);
    }


    //==========================================================================

    /**
     * 查询帖子管理
     * 
     * @param postId 帖子管理主键
     * @return 帖子管理
     */
    @Override
    public Post selectPostByPostId(Long postId)
    {
        return postMapper.selectPostByPostId(postId);
    }

    /**
     * 查询帖子管理列表
     * 
     * @param post 帖子管理
     * @return 帖子管理
     */
    @Override
    public List<Post> selectPostList(Post post)
    {
        return postMapper.selectPostList(post);
    }

    /**
     * 新增帖子管理
     * 
     * @param post 帖子管理
     * @return 结果
     */
    @Override
    public int insertPost(Post post)
    {
        return postMapper.insertPost(post);
    }

    /**
     * 修改帖子管理
     * 
     * @param post 帖子管理
     * @return 结果
     */
    @Override
    public int updatePost(Post post)
    {
        return postMapper.updatePost(post);
    }

    /**
     * 批量删除帖子管理
     * 
     * @param postIds 需要删除的帖子管理主键
     * @return 结果
     */
    @Override
    public int deletePostByPostIds(Long[] postIds)
    {
        return postMapper.deletePostByPostIds(postIds);
    }

    /**
     * 删除帖子管理信息
     * 
     * @param postId 帖子管理主键
     * @return 结果
     */
    @Override
    public int deletePostByPostId(Long postId)
    {
        return postMapper.deletePostByPostId(postId);
    }
}
